﻿using System.ComponentModel.DataAnnotations;
using MediatR;
using SheepCultivation.API.AddliCation.AddressW;
using SheepCultivation.Common.Domain.main;
using SheepCultivation.Common.Domain.Sheep;
using SheepCultivation.Common.Domain.SheepBasicInformation;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.Tip_newspaper
{
    public class PendingPromptsSeachHandler: IRequestHandler<PendingPromptsSeachCommon, Result>
    {
        public readonly IBaseRepository<PendingPrompts> PendingPromptsRepository;
        public readonly IBaseRepository<Columns> BuildingRepository;
        public readonly IBaseRepository<SheepHybridization> SheepHybridizationRepository;
        public PendingPromptsSeachHandler(IBaseRepository<PendingPrompts> pendingPromptsRepository, IBaseRepository<SheepHybridization> sheepHybridizationRepository, IBaseRepository<Columns> buildingRepository)
        {
            PendingPromptsRepository = pendingPromptsRepository;
            SheepHybridizationRepository = sheepHybridizationRepository;
            BuildingRepository = buildingRepository;
        }

        public async Task<Result> Handle(PendingPromptsSeachCommon request, CancellationToken cancellationToken)
        {
            var res = new Result() { Code = (int)CommonEnumCode.error };
            try
            {
                
                res.Code=(int)CommonEnumCode.success;
                var list = (from a in PendingPromptsRepository.GetAll()
                           join b in BuildingRepository.GetAll()
                           on a.Transfer equals b.BuildingId
                           join c in SheepHybridizationRepository.GetAll()
                           on a.ColumnsId equals c.HouseNumber

                            select new PendingPromptsDTO
                           {
                               PendingPromptsId = a.PendingPromptsId,
                               BreedingSheepArchiveId = a.BreedingSheepArchiveId,
                                SheepHybridizationEarNumber = c.SheepHybridizationEarNumber,
                               EmptyDay=a.EmptyDay,
                               BuildingName = c.HouseName,
                               ColumnsName = b.ColumnsName,
                               BreedsStatus = a.BreedsStatus,
                               BreedsDate = a.BreedsDate,
                               PromptName = a.PromptName,
                               SheepId = a.SheepId
                           }).ToList();
                res.Data = list;
                res.Msg = "待配提示";
            }
            catch (Exception ex)
            {

                throw;
            }
            return res;
        }
    }
    public class PendingPromptsDTO
    {
        /// <summary>
        /// 待配提示id
        /// </summary>
        [Key]
        public int PendingPromptsId { get; set; }
        /// <summary>
        /// 种母耳号
        /// </summary>
        public int BreedingSheepArchiveId { get; set; }
        public string SheepHybridizationEarNumber { get; set; }
        /// <summary>
        /// 种母耳号名称
        /// </summary>
        public string Auricle { get; set; }

        /// <summary>
        /// 空怀天数
        /// </summary>
        public int EmptyDay { get; set; }
        /// <summary>
        /// 栋舍
        /// </summary>
        public string BuildingName { get; set; }
        /// <summary>
        /// 栏位
        /// </summary>
        public string ColumnsName { get; set; }
        /// <summary>
        /// 繁殖状态
        /// </summary>
        public bool BreedsStatus { get; set; }
        /// <summary>
        /// 事件日期
        /// </summary>
        public DateTime BreedsDate { get; set; }
        /// <summary>
        /// 待配提示类型
        /// </summary>
        public int PromptName { get; set; }
        /// <summary>
        /// 羊只档案
        /// </summary>
        public int SheepId { get; set; }
    }
}
